package it.koutifaris.travianmap.db.operation.impl;

import it.koutifaris.travianmap.db.DomainUpdaterDAO;
import it.koutifaris.travianmap.db.operation.DomainOperation;
import it.koutifaris.travianmap.db.operation.OperationType;
import it.koutifaris.travianmap.model.DomainElement;

public abstract class DomainUpdaterOperation<T extends DomainElement> implements DomainOperation<T> {

	private DomainUpdaterDAO updaterDAO;
	private OperationType operationType;
	private T domainElement;

	public DomainUpdaterOperation(DomainUpdaterDAO updaterDAO, OperationType operationType, T domainElement) {
		this.updaterDAO = updaterDAO;
		this.operationType = operationType;
		this.domainElement = domainElement;
	}

	protected abstract T doInsert();

	protected abstract T doUpdate();

	protected abstract T doDelete();

	@Override
	public final T doOperation() {
		T element = domainElement;

		switch (getOperationType()) {
		case INSERT:
			element = doInsert();
			break;
		case DELETE:
			element = doDelete();
			break;
		case UPDATE:
			element = doUpdate();
			break;
		}

		return element;
	}

	protected DomainUpdaterDAO getUpdaterDAO() {
		return updaterDAO;
	}

	protected OperationType getOperationType() {
		return operationType;
	}

	protected T getDomainElement() {
		return domainElement;
	}

}
